package com.example.time_bank_demo.controller;


import com.example.time_bank_demo.exception.TimeBankException;
import com.example.time_bank_demo.params.UserCreateParams;
import com.example.time_bank_demo.params.UserLoginParams;
import com.example.time_bank_demo.service.RegisterService;
import com.example.time_bank_demo.service.UserService;
import com.example.time_bank_demo.vo.Result;
import com.example.time_bank_demo.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
@Slf4j
public class UserController {


    private final RegisterService registerService;

    private final UserService userService;

    public UserController(RegisterService registerService, UserService userService) {
        this.registerService = registerService;
        this.userService = userService;
    }

    /**
     * 注册接口
     * @return 注册结果
     */
    @PostMapping("register")
    public Result<UserVo> register(@RequestBody UserCreateParams createParams) {
        //system.out.println("register");
        //{} 是一个占位符 把后面的第n个参数添加到这里打印出来
        log.info("开始注册用户  用户名：{} 手机号： {}", createParams.getName(), createParams.getPhone());
        //和下方这句等价
        // System.out.println("开始注册用户  用户名："+ createParams.getName() + " 手机号： " + createParams.getPhone());
        UserVo register = null;
        try {
            register = registerService.register(createParams);
        } catch (TimeBankException e) {
            return  Result.error(e.getCode(), e.getMessage());
        }
        return Result.success(register);
    }

    //T t  网络层



    /**
     * 登录接口
     * @return 登录结果
     */
    @PostMapping("login")
    public Result<UserVo> login(@RequestBody UserLoginParams loginParams) {
        try {
            UserVo login = userService.login(loginParams.getPhone(), loginParams.getCode());
            return Result.success(login);
        } catch (TimeBankException e) {
            return  Result.error(e.getCode(), e.getMessage());
        }

    }
}
